package edu.gatech.cs2340.group7.wheresmystuffapp.model;

import java.util.List;

import android.app.Activity;
import edu.gatech.cs2340.group7.wheresmystuffapp.database.WMSSQLiteDatabase;

public class DatabaseHelper {
	

	WMSSQLiteDatabase database;
	private Activity activity;
	
	public DatabaseHelper(Activity a){
		activity = a;
		database = new WMSSQLiteDatabase(activity);
		
	}

	/**
	 * Adds an item to the SQLite Database
	 * @param i the item to be added
	 * @return -1 if there was an error in adding the item
	 */
	public long addItem(Item i) {
		long out = -1;
		database.open();
		out = database.addItem(i);
		System.out.println(i.getName() + " added to SQLite Database");
		database.close();
		return out;
		
	}

	/**
	 * @return a list containing all of the items added to the database
	 */
	public List<Item> getAllItems() {
		database.open();
		List<Item> listItems = database.getAllItems();
		database.close();
		return listItems;
	}

	/**
	 * Adds an account to the SQLite Database
	 * @param account the account to be added
	 */
	public void addAccount(Account account) {
		database.open();
		database.addAccount(account);
		database.close();
		System.out.println(account.getEmail() + " added to SQLite Database");
	}
	
	/**
	 * Gets an account with a certain username
	 * @param email the username of the account to find
	 * @return the account with the specified username
	 */
	public Account getAccount(String email){
		database.open();
		System.out.println("Database open, getting account " + email);
		Account a = database.getAccount(email);
		System.out.println("Got account: " + a.getEmail());
		database.close();
		return a;
	}
	
	/**
	 * @return a list of all the accounts stored in the database
	 */
	public List<Account> getAllAccounts() {
		database.open();
		List<Account> out = database.getAllAccounts();
		database.close();
		return out;
	}

	/**
	 * 
	 * @param username Username to check if it exists in the database
	 * @return True if the user exists in the database and false otherwise
	 */
	public boolean accountExists(String username) {
		database.open();
		boolean out = database.accountExists(username);
		database.close();
		return out;
	}
	
	/**
	 * Updates the account in the database
	 * @param account Account to be updated in the database
	 */
	public void updateAccount(Account account) {
		database.open();
		database.updateAccount(account);
		database.close();
	}
	
	public List<Item> getAllItemsForUser(String username) {
		database.open();
		List<Item> out = null;
		out = database.getItems(username);
		database.close();
		return out;
	}
	
	/**
	 * Gets the account from the database, changes the password, and updates the database
	 * @param username Username of account to be updated
	 * @param oldPassword Old password for the user
	 * @param newPassword New password for the user
	 * @return True if change was successful and false otherwise
	 */
	public boolean changeAccountPassword(String username, String oldPassword, String newPassword) {
		boolean out = false;
		database.open();
		Account account = database.getAccount(username);
		out = account.changePassword(oldPassword, newPassword);
		database.updateAccount(account);
		
		return out;
	}
	
	public boolean deleteAccount(String username) {
		boolean out = false;
		database.open();
		Account account = database.deleteAccount(username);
		
		if(account != null) {
			out = true;
		}
		
		return out;
	}
}
